package com.thread.test1;

/**
 * @description: TODO 类描述
 * @author: YUANG
 * @date: 2022/3/6
 **/
public class ThreadSyncTest {
    public static void main(String[] args) throws InterruptedException {
        AddThread a = new AddThread();
        DecThread d = new DecThread();
        a.start();
        d.start();
        a.join();
        d.join();
        System.out.println(Counter.count);
    }
}

class Counter{
    public static int count = 0;
    public static final Object lock = new Object();
}

class AddThread extends Thread{
    @Override
    public void run() {
        for (int i = 0; i < 10000; i++) {
            synchronized (Counter.lock) {
                Counter.count++;
            }
        }
    }
}

class DecThread extends Thread {
    @Override
    public void run() {
        for (int i = 0; i < 10000; i++) {
            synchronized (Counter.lock) {
                Counter.count--;
            }
        }
    }
}
